<?php

namespace App\Console\Commands\SiteLocation;

use App\Models\Common\ConfigModel;
use App\Models\Common\HolidayModel;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class ChangeSiteManagerUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'siteLocation:change_site_manager_user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '修改选址负责人';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     */
    public function handle()
    {
        /**
         * 选址审批人-
         * 1、工作日期间（09:00-18:00）：切换成碧瑶 rmsUserId = 5590
         * 2、非工作时间期间（18:00-到次日09:00）和非工作日（如周六周日）：切换成赤瞳 rmsUserId = 49
         */

        $biyaoRmsUserId   = 5590;  // 碧瑶
        $chitongRmsUserId = 49;    // 赤瞳


        $configModel = ConfigModel::query()
            ->where('module', 'siteLocation')
            ->where('name', ConfigModel::SITE_MANAGER)
            ->first();

        if ($configModel == null) {
            return;
        }

        $date          = Carbon::now();
        $managerUserId = (int)$configModel->value('value');

        // 判断今天是否是工作日
        $isWorkday = HolidayModel::checkWork($date->format('Ymd'));

        if ($date->hour == 9 && $isWorkday) {
            // 是工作日并且是9点 切换成碧瑶
            $managerUserId = $biyaoRmsUserId;
        }

        if ($date->hour == 18 || !$isWorkday) {
            // 18点以后或者非工作日 切换成赤瞳
            $managerUserId = $chitongRmsUserId;
        }

        // 如果新的处理人和之前的相同就结束
        if ($managerUserId == (int)$configModel->value('value')) {
            return;
        }

        $configModel->value = [$managerUserId];
        $configModel->save();

        // 刷新选址处理人
        Artisan::call('openshop:scrub_site_location_handler');

    }


    /**
     * 测试
     * @return void
     */
    public function test()
    {

        $dateInfoList = [
            [
                'ymd'  => '20240312',
                'hour' => 9
            ], [
                'ymd'  => '20240312',
                'hour' => 18
            ], [
                'ymd'  => '20240313',
                'hour' => 9
            ], [
                'ymd'  => '20240313',
                'hour' => 18
            ], [
                'ymd'  => '20240314',
                'hour' => 9
            ], [
                'ymd'  => '20240314',
                'hour' => 18
            ], [
                'ymd'  => '20240315',
                'hour' => 9
            ], [
                'ymd'  => '20240315',
                'hour' => 18
            ], [
                'ymd'  => '20240316',
                'hour' => 9
            ], [
                'ymd'  => '20240316',
                'hour' => 18
            ], [
                'ymd'  => '20240317',
                'hour' => 9
            ], [
                'ymd'  => '20240317',
                'hour' => 18
            ], [
                'ymd'  => '20240318',
                'hour' => 9
            ], [
                'ymd'  => '20240318',
                'hour' => 18
            ]
        ];


        foreach ($dateInfoList as $dateInfo) {
            // 判断今天是否是工作日
            $isWorkday = HolidayModel::checkWork($dateInfo['ymd']);

            if ($dateInfo['hour'] == 9 && $isWorkday) {
                // 是工作日并且是9点 切换成碧瑶
                $this->info($dateInfo['ymd'] . ' 星期' . Carbon::createFromFormat('Ymd', $dateInfo['ymd'])->dayOfWeekIso . ' ' . $dateInfo['hour'] . '点 >>>> 碧瑶');
            }

            if ($dateInfo['hour'] == 18 || !$isWorkday) {
                // 18点以后或者非工作日 切换成赤瞳
                $this->info($dateInfo['ymd'] . ' 星期' . Carbon::createFromFormat('Ymd', $dateInfo['ymd'])->dayOfWeekIso . ' ' . $dateInfo['hour'] . '点 >>>> 赤瞳');
            }
        }
    }
}
